package com.luo.demo;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;

import com.luo.utils.PhotoBitmapUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/5/4.
 */

public class CameraActivity extends Activity {
    private static final int OPEN_CAMERA = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addBitmapShoots();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 获取相机拍照返回
        if (resultCode == Activity.RESULT_OK && requestCode == OPEN_CAMERA) {
            // 得到修复后的照片路径
            SystemClock.sleep(2000);
            String filepath = PhotoBitmapUtils.amendRotatePhoto(fileName, getApplicationContext());
        }
    }

    String fileName = "";

    /**
     * 启动相机拍照
     */
    private void addBitmapShoots() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 设置图片要保存的 根路径+文件名
        fileName = PhotoBitmapUtils.getPhotoFileName(getApplicationContext());
        File file = new File(fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());
        //方式一
        Uri uri = Uri.fromFile(file);
        //方式二
        ContentValues contentValues = new ContentValues(1);
        contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
//        uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        //方式三
        uri = FileProvider.getUriForFile(getApplicationContext(), "com.luo.demo.fileprovider", file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

        startActivityForResult(intent, OPEN_CAMERA);
    }

}
